<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    showFooter
    :title="getTitle"
    width="50%"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm"/>
  </BasicDrawer>
</template>
<script lang="ts">
  import {defineComponent, ref, computed, unref} from 'vue';
  import {BasicForm, useForm} from '/@/components/Form/index';
  import {BasicDrawer, useDrawerInner} from '/@/components/Drawer';
  import {formSchema} from './refund.data';
  import {getById} from './refund.api';

  export default defineComponent({
    name: 'RefundOrderDrawer',
    components: {BasicDrawer, BasicForm},
    emits: ['success', 'register'],
    setup() {
      const isUpdate = ref(true);

      const [registerForm, {resetFields, setFieldsValue}] = useForm({
        labelWidth: 100,
        schemas: formSchema,
        showActionButtonGroup: false,
        baseColProps: {lg: 12, md: 24},
      });

      const detailData = ref();
      const [registerDrawer, {setDrawerProps, closeDrawer}] = useDrawerInner(async (data) => {
        resetFields();
        setDrawerProps({confirmLoading: false});
        isUpdate.value = !!data?.isUpdate;
        detailData.value = await getById(data.record.id);

        if (unref(isUpdate)) {
          setFieldsValue({
            ...detailData,
          });
        }
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '退款订单' : '退款订单'));

      async function handleSubmit() {
        closeDrawer();
      }

      return {
        registerDrawer,
        registerForm,
        handleSubmit,
        getTitle,
      };
    },
  });
</script>
